const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const { PurgeCSSPlugin } = require('purgecss-webpack-plugin');
const glob = require('glob');
const path = require('path');
const  webpack  = require('webpack');
module.exports = {
  devtool: false, //'source-map',
  mode: 'development',
  // 优化配置
  optimization: {
    // treeshaking
    // 倒入模块分析哪些函数 有被使用 哪些没有被使用
    usedExports: true, // production 默认开启

    // 生成的chunkid算法
    // named  development 推荐
    // 默认的 deterministic production 推荐
    chunkIds: 'deterministic',
    // runtimeChunk: {
    //   // 脚手架不配置了
    //   name: 'runtime'
    // },
    splitChunks: {
      // 分包
      chunks: 'all',
      // maxSize: 20 * 1024,
      // minSize: 10 * 1024,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          filename: 'js/[id]_verdors.js'
        },
        routers: {
          test: /router/,
          filename: 'js/[id]router_verdors.js'
        }
      }
    },
    // 开启 develop压缩
    // minimize: true,
    minimizer: [
      new TerserPlugin({ extractComments: false, parallel: 4 }),
      new CssMinimizerWebpackPlugin({ parallel: 4 })
    ]
  },
  plugins: [
    // 提取css
    new MiniCssExtractPlugin({
      filename: 'css/[name]_[hash:6].css',
      chunkFilename: 'css/[name]_chunk.css'
    }),
    // css tree shaking
    new PurgeCSSPlugin({
      paths: glob.sync(`${path.resolve(__dirname, '../src')}/**/*`, {
        nodir: true
      })
    }),
    // 作用域提升
    // new webpack.optimize.ModuleConcatenationPlugin()
  ]
};
